package org.jdcenter.server.xb.parser;

import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jdcenter.server.xb.metadata.MateData;
/**
 * @author miao
 * @since 2008-11-27
 *
 */
public abstract class AbstractParser<T extends MateData> {
	
	private static Log log = LogFactory.getLog(AbstractParser.class);
	
	protected final static String beanTag = "bean";
	
	protected URL xmlURL = null;
	private SAXReader reader = null;
	private Document doc = null;
	protected Element root = null;
	
	private void initXML(URL xmlURL) throws DocumentException{
		this.xmlURL = xmlURL;
		reader = new SAXReader();
		doc = reader.read(xmlURL);
		root = doc.getRootElement();
	}
	
	public T parse(URL xmlURL) throws DocumentException{
		if(xmlURL == null){
			throw new IllegalArgumentException("path of URL can not be null");
		}
		log.info("parse "+xmlURL.toString());
		initXML(xmlURL);
		
		return parseXML();
	}
	
	protected abstract T parseXML();
	
	

}
